<template>
  <!-- 邮件发送记录表表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="800px"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="160">
      <el-row v-show="!props.isReadonly">
        <el-col :span="12">
          <el-form-item label="工段id" prop="workshopSectionId">
            <el-input v-if="!props.isReadonly" v-model="formData.workshopSectionId" placeholder="请输入工段id"
                      clearable/>

            <span v-else>{{ formData.workshopSectionId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工序id" prop="processId">
            <el-input v-if="!props.isReadonly" v-model="formData.processId" placeholder="请输入工序id" clearable/>

            <span v-else>{{ formData.processId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生产线id" prop="processEquipId">
            <el-input v-if="!props.isReadonly" v-model="formData.processEquipId" placeholder="请输入生产线id"
                      clearable/>

            <span v-else>{{ formData.processEquipId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产品型号id" prop="modelId">
            <el-input v-if="!props.isReadonly" v-model="formData.modelId" placeholder="请输入产品型号id" clearable/>

            <span v-else>{{ formData.modelId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="检具id" prop="gageEquipMarkId">
            <el-input v-if="!props.isReadonly" v-model="formData.gageEquipMarkId" placeholder="请输入检具id" clearable/>

            <span v-else>{{ formData.gageEquipMarkId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="程序名" prop="programName">
            <el-input v-if="!props.isReadonly" v-model="formData.programName" placeholder="请输入程序名" clearable/>

            <span v-else>{{ formData.programName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="参数类型" prop="processDataType">
            <el-input v-if="!props.isReadonly" v-model="formData.processDataType" placeholder="请输入参数类型"
                      clearable/>

            <span v-else>{{ formData.processDataType }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="参数id" prop="parameterId">
            <el-input v-if="!props.isReadonly" v-model="formData.parameterId" placeholder="请输入参数id" clearable/>

            <span v-else>{{ formData.parameterId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="发送时间" prop="sendTime">
            <el-input v-if="!props.isReadonly" v-model="formData.sendTime" placeholder="请输入发送时间" clearable/>

            <span v-else>{{ formData.sendTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生成工单id" prop="orderId">
            <el-input v-if="!props.isReadonly" v-model="formData.orderId" placeholder="请输入生成工单id" clearable/>

            <span v-else>{{ formData.orderId }}</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-descriptions
        v-show="props.isReadonly"
        class="margin-top"
        :column="2"
        size="large"
        border
      >
        <el-descriptions-item label="工段">
          <div style="width: 100%; word-break: break-all">
            {{ formData.workshopSectionCode }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item label="工序">
          <div style="width: 100%; word-break: break-all">
            {{ formData.processCode }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item label="生产线">
          <div style="width: 100%; word-break: break-all">
            {{ formData.processEquipCode }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item label="型号描述">
          <div style="width: 100%; word-break: break-all">
            {{ formData.modelName }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item label="产品型号">
          <div style="width: 100%; word-break: break-all">
            {{ formData.modelCode }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item label="程序名">
          <div style="width: 100%; word-break: break-all">
            {{ formData.programName }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item label="参数类型">
          <div style="width: 100%; word-break: break-all">
            {{ useDictionary("processDataType", formData.processDataType) || ""}}
          </div>
        </el-descriptions-item>
        <el-descriptions-item label="参数名称">
          <div style="width: 100%; word-break: break-all">
            {{ formData.parameterDesc }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item label="邮件时间">
          <div style="width: 100%; word-break: break-all">
            {{ formData.sendTime }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item label="邮件接收人">
          <div style="width: 100%; word-break: break-all">
            {{ formData.recipientUserName }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item label="邮件内容" :span="2">
          <div style="width: 100%; word-break: break-all">
            {{ formData.content }}
          </div>
        </el-descriptions-item>
      </el-descriptions>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button v-if="!props.isReadonly" type="primary" :loading="loading" @click="handleSubmit">确 定</el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import {useDictStoreHook} from "@/store/modules/dictionary";
import {useDictionary} from "../../../../utils/useTable"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "PCMPSMMAILSENDRECORDADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  workshopSectionId: [],
  processId: [],
  processEquipId: [],
  modelId: [],
  gageEquipMarkId: [],
  programName: [],
  processDataType: [],
  parameterId: [],
  sendTime: [],
  orderId: [],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  version: "",
  type: "",
  workshopSectionId: "",
  workshopSectionCode: "",
  processId: "",
  processCode: "",
  processEquipId: "",
  processEquipCode: "",
  modelId: "",
  modelName: "",
  modelCode: "",
  gageEquipMarkId: "",
  programName: "",
  processDataType: "",
  parameterId: "",
  parameterDesc: "",
  sendTime: "",
  recipientUserName: "",
  content: "",
  orderId: "",
  enabled: "",
  deleted: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = ""
    if (newVal?.id) {
      const {
        id,
        version,
        type,
        workshopSectionId,
        workshopSectionCode,
        processId,
        processCode,
        processEquipId,
        processEquipCode,
        modelId,
        modelName,
        modelCode,
        gageEquipMarkId,
        programName,
        processDataType,
        parameterId,
        parameterDesc,
        sendTime,
        content,
        recipientUserName,
        orderId,
        enabled,
        deleted,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      } = newVal;
      Object.assign(formData, {
        id,
        version,
        type,
        workshopSectionId,
        workshopSectionCode,
        processId,
        processCode,
        processEquipId,
        processEquipCode,
        modelId,
        modelName,
        modelCode,
        gageEquipMarkId,
        programName,
        processDataType,
        parameterId,
        parameterDesc,
        sendTime,
        content,
        recipientUserName,
        orderId,
        enabled,
        deleted,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = ""
  formData.version = ""
  formData.type = ""
  formData.workshopSectionId = ""
  formData.workshopSectionCode = ""
  formData.processId = ""
  formData.processCode = ""
  formData.processEquipId = ""
  formData.processEquipCode = ""
  formData.modelId = ""
  formData.modelName = ""
  formData.modelCode = ""
  formData.gageEquipMarkId = ""
  formData.programName = ""
  formData.processDataType = ""
  formData.parameterId = ""
  formData.parameterDesc = ""
  formData.sendTime = ""
  formData.content = ""
  formData.recipientUserName = ""
  formData.orderId = ""
  formData.enabled = ""
  formData.deleted = ""
  formData.remarks = ""
  formData.createTime = ""
  formData.createdUserId = ""
  formData.createdUserName = ""
  formData.lastUpdateTime = ""
  formData.lastUpdatedUserId = ""
  formData.lastUpdatedUserName = ""
}

const loading = ref(false);

/** 邮件发送记录表保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/dmpDosPcm/psmmailsendrecord/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}


// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});


</script>
